/************************************************************************
 * ClassName:   ConditionVariable    
 * Namespace:   why::synchronization
 * FileName:    ConditionVariable.h ConditionVariable.cpp  
 * Date:        2015-11-6
 * Author:      wanghongyun
 * Description: Use to inform other threads some condition coming into existence
 ************************************************************************/

#ifndef _WHY_SYNCHRONIZATION_CONDITIONVARIABLE_H_
#define _WHY_SYNCHRONIZATION_CONDITIONVARIABLE_H_

#include "basictypes.h"
#if defined (OS_WIN)
#include <Windows.h>
#else 
#include <pthread.h>
#endif 

namespace why
{
    namespace synchronization
    {
#if defined (OS_WIN)
        typedef CRITICAL_SECTION   UserMutex;
        typedef CONDITION_VARIABLE ConditionVariableType;
#else 
        typedef pthread_mutex_t UserMutex;
        typedef pthread_cond_t  ConditionVariableType;
#endif 

        class ConditionVariable
        {
        public:
            explicit ConditionVariable(void);
            virtual ~ConditionVariable(void);

        public:
            void Wait();                                // Wait permanently until the condition come into existence
            bool TimedWait(uint32 wait_milliseconds);   // Wait the condition coming into existence or time is out
            void Signal();                              // Wake up one thread that is waiting the condition
            void Broadcast();                           // Wake up all threads that are waiting the condition

        private:
            ConditionVariableType condition_;           // Condition variable
            UserMutex user_mutex_;                      // The mutex to protect the condition_

            DISALLOW_COPY_AND_ASSIGN(ConditionVariable);

        };
    }
}

#endif
